# @File    : android.py
# @Project : JBL_Android&iOS
from threading import Thread
from airtest.core.api import *
from common.logUtil import info_a
from Android.cancel_ota import CancelOta
from Android.cycle_ota import CycleOta
from Android.kill_ota import KillOta
from Android.upgrade import Upgrade
from common.yamlUtil import YamlUtil


class AndroidUtil:

    conf = YamlUtil("data.yaml").read_yaml()

    # 升降级
    def promotion_demotion(self, num):
        def promotion():
            stop_app(f"{self.conf['app_android']}")
            info_a("升降级测试开始...")
            sleep(2)
            info_a(f"压测次数为: {num}")
            start_app(f"{self.conf['app_android']}")
            ota = Upgrade()
            ota.ota_updown_grade(num)
            sleep(5)
            stop_app(f"{self.conf['app_android']}")
            info_a("升降级测试结束...")

        promotion_thread = Thread(target=promotion)
        promotion_thread.start()

    # 取消升级
    def cancel_ota(self, num):
        def cancel():
            stop_app(f"{self.conf['app_android']}")
            info_a("取消升级测试开始...")
            sleep(2)
            info_a(f"压测次数为: {num}")
            start_app(f"{self.conf['app_android']}")
            ota = CancelOta()
            ota.ota_cancel(num)
            sleep(5)
            stop_app(f"{self.conf['app_android']}")
            info_a("取消升级测试结束...")

        cancel_thread = Thread(target=cancel)
        cancel_thread.start()

    # 中断升级
    def kill_ota(self, num):
        def kill():
            stop_app(f"{self.conf['app_android']}")
            info_a("中断升级测试开始...")
            sleep(2)
            info_a(f"压测次数为: {num}")
            start_app(f"{self.conf['app_android']}")
            ota = KillOta()
            ota.ota_kill_app(num)
            sleep(5)
            stop_app(f"{self.conf['app_android']}")
            info_a("中断升级测试结束...")

        kill_thread = Thread(target=kill)
        kill_thread.start()

    # 循环升级
    def cycle_upgrade(self, num):
        def cycle():
            stop_app(f"{self.conf['app_android']}")
            info_a("循环升级测试开始...")
            sleep(2)
            info_a(f"压测次数为: {num}")
            start_app(f"{self.conf['app_android']}")
            # 循环升级方法待编写
            ota = CycleOta()
            ota.cycle_ota(num)
            sleep(5)
            stop_app(f"{self.conf['app_android']}")
            info_a("循环升级测试结束...")

        cycle_thread = Thread(target=cycle)
        cycle_thread.start()

    # def read_process(self):
    #     def process():
    #         ota = Upgrade()
    #         ota.check_process()
    #
    #     process_thread = Thread(target=process)
    #     process_thread.start()
